/****************************************************************************
*                                                                           *
* Copyright (c) RSC. All rights reserved(2010).								*
* Developer: Marius Ionescu    						                        *
*                                                                           *
****************************************************************************/

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.Security;
using System.Web.UI.WebControls;
using NS_Controls;
using System.Web.Services;
using System.Web.Script.Services;

public partial class Pacient : SessionPageBase
{
	public string idPacient
	{
		get { return (this.ViewState["idPacient"] ?? "").ToString(); }
		set
		{
			this.ViewState["idPacient"] = value;
			UpdateTabs();
		}
	}

	protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
			if (!IsPostBack)
				Session["customQueryString"] = "";
            idPacient = Request.QueryString["pacientID"] ?? "";
			UpdateTabs();
			if (idPacient != "")
				this.Master.strPacient = "(FISA PACIENT: " + idPacient + ")";
		}
        catch (Exception ex)
        {
            this.SetMessage("[@'Pacient']" + ex.Message, true);
        }
    }

    private void UpdateTabs()
	{
		string requestedAction = Request.QueryString["action"] ?? "";
		int lastActiveTab = topSideTabs.Tabs.Count - 1;
		if (idPacient.Length == 0)
		{
			if (!requestedAction.ToLower().Equals("new"))
				Response.Redirect(Request.Path + "?action=new");
			lastActiveTab = TabbedDetails.pacientTabIndex;
			topSideTabs.Tabs[TabbedDetails.admissionTabIndex].HeaderText = DisableText(topSideTabs.Tabs[1].HeaderText);
			topSideTabs.Tabs[TabbedDetails.checkinTabIndex].Visible = false;
			topSideTabs.Tabs[TabbedDetails.checkupTabIndex].Visible = false;
			topSideTabs.Tabs[TabbedDetails.checkoutTabIndex].Visible = false;
		}
		if (requestedAction.ToLower().Equals("admit") && (TabbedDetails.admissionTabIndex <= lastActiveTab))
			topSideTabs.ActiveTabIndex = TabbedDetails.admissionTabIndex;
		if (requestedAction.ToLower().Equals("checkin") && (TabbedDetails.checkinTabIndex <= lastActiveTab))
			topSideTabs.ActiveTabIndex = TabbedDetails.checkinTabIndex;
	}

	private string prefixFormatter = "[<i><span style=\"color:red\">";
	private string suffixFormatter = "</span></i>]";
	private string DisableText(string input)
	{
		if (input.StartsWith("[") && input.EndsWith("]"))
			return input;
		return prefixFormatter + input + suffixFormatter;
	}
	private string EnableText(string input)
	{
		return input.Replace(prefixFormatter, "").Replace(suffixFormatter, "");
	}

	protected void topSideTabs_ActiveTabChanged(object sender, EventArgs e)
	{
		//NB: this has the interesting side-effect of leaving the selected tab shown for the duration of the round-trip! :D
		if (idPacient == "")
			if (topSideTabs.ActiveTabIndex > TabbedDetails.pacientTabIndex)
				topSideTabs.ActiveTabIndex = TabbedDetails.pacientTabIndex;

		string requestedAction = Request.QueryString["action"] ?? "";
		//if (!requestedAction.Equals(TabbedDetails.action[topSideTabs.ActiveTabIndex]))
		{
			System.Text.StringBuilder rebuiltQueryString = new System.Text.StringBuilder("?action=" + TabbedDetails.action[topSideTabs.ActiveTabIndex]);
			foreach (string k in Request.QueryString.Keys)
				if (!k.ToLower().Equals("action"))
				{
					rebuiltQueryString.Append('&');
					rebuiltQueryString.Append(k);
					rebuiltQueryString.Append('=');
					rebuiltQueryString.Append(Request.QueryString[k]);
				}
			string[] qs = new string[2 + 2 * Request.QueryString.Keys.Count];
			int i = 0;
			foreach (string key in Request.QueryString.Keys) { qs[i++] = key; qs[i++] = Request.QueryString[key]; }
			qs[i++] = "action"; qs[i++] = TabbedDetails.action[topSideTabs.ActiveTabIndex];
			TabbedDetails.ShowUrl(editPacient, qs);
			//TabbedDetails.SetUrl(this, Request.Path + rebuiltQueryString.ToString()); //if (!~) SetMessage("Not found!", false);
			//Response.Redirect(Request.Path + rebuiltQueryString.ToString());
		}
	}

    [WebMethod, ScriptMethod]
    static public string SendMessage(string msg)
    {
        try
        {
            Presentation pac = new Presentation();
            //pac.hideModalPopUp();
        }
        catch (Exception ex)
        {
            SessionPageBase.SetMessageStatic(ex.Message, true);
        }
        return "";
    }
}
